package com.xu.crawler.collector.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.xu.crawler.collector.AbstractCollector;
import com.xu.crawler.config.timeWaitingStrategy.TimeWaitingStrategy;
import com.xu.crawler.util.RequestParamBuilder;
import com.xu.crawler.enums.URLMapperEnum;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by clear on 2018/7/10.
 */
public class SouthChinaCollector extends AbstractCollector {


    public SouthChinaCollector(TimeWaitingStrategy strategy) throws RemoteException {
        super(strategy, URLMapperEnum.SOUTH_CHINA_HOST.toString());
    }

    public static void main(String[] args) {
        try {
//            Document doc = Jsoup.connect(URLMapperEnum.SOUTH_CHINA_HOST.toString()).get();
//            Document doc = Jsoup.connect("https://www.scmp.com/sitemap_news.xml").get();
//            System.out.println(doc.title());
            SouthChinaCollector southChinaCollector = new SouthChinaCollector(null);
            southChinaCollector.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected Object collectLogic() throws Exception {
//        String target = URLMapperEnum.SOUTH_CHINA_HOST.toString();
        String target = "https://www.scmp.com/sitemap_news.xml";
        List<JsonNode> nodeList = new ArrayList<>();

        JsonNode node;
        try {
            int loopTime = 1;
            while (strategy.retryTimes() > loopTime) {
                try {
                    RequestParamBuilder builder = new RequestParamBuilder(target);
                    String html = request(builder.build());

                    Document doc = Jsoup.parse(html);
                    String msg = doc.html();// 获取原生页
                    System.out.println(msg);
//                    Document document = Jsoup.parse(html);
//                    String json = request(url);
//                    node = mapper.readTree(json);
//                    nodeList.add(node);
                    break;
                } catch (Exception e) {
                    log.error("collect {} fail, Network busy Retrying -> {} times",target, loopTime);
                    if (!(e instanceof IOException)) throw e;
                    this.strategy.waiting(loopTime++);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return processNode(nodeList);
    }

    private List<URL> processNode(List<JsonNode> nodeList) {

        List<URL> res = new ArrayList<>();

        for (JsonNode node : nodeList) {
            try {
                for (JsonNode jsonNode : node) {
                    String suffix = jsonNode.get("target").asText();
                    String path = URLMapperEnum.SOUTH_CHINA_HOST.toString() + suffix;
                    res.add(new URL(path));
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }

        return res;

    }
}
